<template>
    <view>
        <view class="container">
            <image :src="userinfo.fxbg" mode="widthFix" @load="imgHeight" style="width: 100%"></image>
        </view>

        <view class="fxbg">
            <view class="share" @tap="formSubmit">海报分享</view>

            <view class="sharescan">
                <view @tap="toFxRule">邀请规则</view>
                <view @tap="toMyteam">我的邀请</view>
            </view>
        </view>

        <view class="imagePathBox" v-if="maskHidden != false">
            <image :src="imagePath" class="shengcheng"></image>
            <button class="baocun" @tap="baocun">保存相册，分享到朋友圈</button>
        </view>

        <view v-if="maskHidden != false" class="mask"></view>
        <view class="canvas-box">
            <canvas style="width: 375px; height: 667px; position: fixed; top: 9999px" canvas-id="mycanvas" />
        </view>
    </view>
</template>

<script>
import { Agent } from '../../model/agent-model.js';
var agent = new Agent();
import { My } from '../my/my-model.js';
var my = new My();
export default {
    data() {
        return {
            /* 生成二维码参数 */
            img: '/static/imgs/icon/gobg.jpg',

            wechat: '../../imgs/icon/wechat.png',
            quan: '../../imgs/icon/quan.png',
            code: 'E7AI98',
            inputValue: '',
            maskHidden: false,
            name: '',
            touxiang: '',
            code: 'E7A93C',
            qrcode: '',

            gobg: '',
            /* 生成二维码参数 */ userinfo: {
                fxbg: ''
            },

            imagePath: '',
            canvasHidden: false
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        var that = this;
        var title = '邀请好友';
        uni.setNavigationBarTitle({
            title: title
        });
        var params = {};
        my.getWxUserInfo((data) => {
            that.qrcode = data.userinfo.qrcode;
            that.gobg = data.userinfo.sharebg;
            that.setData({
                userinfo: data.userinfo
            });
            uni.hideNavigationBarLoading(); //完成停止加载
            uni.stopPullDownRefresh();
        }, params);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        toIndex: function () {
            uni.redirectTo({
                url: '/pages/index/index'
            });
        },

        toFindjob: function (e) {
            var that = this;
            uni.redirectTo({
                url: '/pages/findjob/index'
            });
        },

        toMyinvate: function (e) {
            var that = this;
            uni.redirectTo({
                url: '/pages/myinvate/index'
            });
        },

        toSysmsg: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/sysmsg/index'
            });
        },

        toMyuser: function () {
            uni.redirectTo({
                url: '/pages/user/index'
            });
        },

        toMyteam: function () {
            uni.redirectTo({
                url: '/pages/myteam/index'
            });
        },

        toFxRule: function (e) {
            uni.navigateTo({
                url: '/pages/fxrule/index'
            });
        },

        createNewImg: function () {
            var that = this;
            var context = uni.createCanvasContext('mycanvas');
            context.setFillStyle('#ffe200');
            context.fillRect(0, 0, 375, 667);
            var path = that.gobg;
            context.drawImage(that.gobgfile, 0, 0, 375, 667);
            var path1 = that.touxiang;
            var path4 = '/static/imgs/icon/wenziBg.png';
            var path5 = '../../imgs/icon/wenxin.png';
            var name = that.name;
            //绘制名字
            context.setFontSize(24);
            context.setFillStyle('#333333');
            context.setTextAlign('center');
            context.fillText(name, 185, 340);
            context.stroke();

            //绘制左下角文字背景图
            context.drawImage(path4, 25, 520, 184, 82);
            context.setFontSize(12);
            context.setFillStyle('#333');
            context.setTextAlign('left');
            context.fillText('进入小程序查看信息', 35, 540);
            context.stroke();
            context.setFontSize(12);
            context.setFillStyle('#333');
            context.setTextAlign('left');
            context.fillText(',分享赚钱正在等着', 35, 560);
            context.stroke();
            context.setFontSize(12);
            context.setFillStyle('#333');
            context.setTextAlign('left');
            context.fillText('你来哦~', 35, 580);
            context.stroke();
            //绘制右下角扫码提示语

            context.drawImage(that.myqrcodefile, 243, 495, 100, 100);
            context.drawImage(path5, 248, 598, 90, 25);
            //绘制头像
            context.arc(186, 246, 50, 0, 2 * Math.PI); //画出圆
            context.strokeStyle = '#ffe200';
            context.clip(); //裁剪上面的圆形
            context.drawImage(path1, 136, 196, 100, 100); // 在刚刚裁剪的园上画图
            context.draw();
            //将生成好的图片保存到本地，需要延迟一会，绘制期间耗时

            setTimeout(function () {
                uni.hideToast();
                that.drawAfter();
                that.setData({
                    maskHidden: true
                });
            }, 200);
        },

        //点击保存到相册
        baocun: function () {
            var that = this;
            uni.saveImageToPhotosAlbum({
                filePath: that.imagePath,
                success(res) {
                    uni.showModal({
                        content: '图片已保存到相册，赶紧晒一下吧~',
                        showCancel: false,
                        confirmText: '好的',
                        confirmColor: '#333',
                        success: function (res) {
                            if (res.confirm) {
                                console.log('用户点击确定');
                                /* 该隐藏的隐藏 */
                                that.setData({
                                    maskHidden: false
                                });
                            }
                        },
                        fail: function (res) {
                            console.log(11111);
                        }
                    });
                }
            });
        },

        formSubmit: function (e) {
            var that = this;
            this.setData({
                maskHidden: false
            });
            uni.showToast({
                title: '正在生成中...',
                icon: 'loading',
                duration: 1000
            });
            uni.downloadFile({
                url: that.qrcode,
                success: function (res) {
                    console.log(res.tempFilePath);
                    that.myqrcodefile = res.tempFilePath;
                    console.log(that.myqrcodefile);
                    uni.downloadFile({
                        url: that.gobg,
                        success: function (res2) {
                            that.gobgfile = res2.tempFilePath;
                            that.createNewImg();
                        }
                    });
                }
            });
        },

        drawAfter: function () {
            var that = this;
            uni.canvasToTempFilePath({
                canvasId: 'mycanvas',
                success: function (res) {
                    var tempFilePath = res.tempFilePath;
                    that.setData({
                        imagePath: tempFilePath,
                        canvasHidden: true
                    });
                },
                fail: function (res) {
                    console.log(res);
                }
            });
        },

        toMyfxinvate: function () {
            uni.navigateTo({
                url: '/pages/myfxinvate/index'
            });
        },

        toAgentsort: function () {
            uni.navigateTo({
                url: '/pages/agentsort/index'
            });
        },

        imgHeight() {
            console.log('占位：函数 imgHeight 未声明');
        }
    }
};
</script>
<style>
.container {
    background-color: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;

    height: 1200rpx;
}

.fxbg {
    height: 600rpx;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 20rpx;

    position: fixed;
    z-index: 99;
    left: 0;
    bottom: 0;
    background-image: url('https://api.site100.cn/fxbg_bt.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
}
.share {
    background-color: #fe8f00;
    height: 110rpx;
    width: 680rpx;
    border-radius: 60rpx;
    margin-top: 100rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    font-size: 22px;
}

.sharescan {
    height: 110rpx;
    width: 680rpx;

    margin-top: 60rpx;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    color: #fff;
    font-size: 22px;
}
.sharescan view {
    color: #fe8f00;
    border: 1rpx solid #fe8f00;
    height: 110rpx;
    width: 330rpx;
    border-radius: 60rpx;
    display: flex;
    justify-content: center;
    align-items: center;
}

.shareintro {
    height: 110rpx;
    width: 680rpx;

    margin-top: 50rpx;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    color: #666;
    font-size: 18px;
}

.imagePathBox {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10000;
}
.shengcheng {
    width: 80%;
    height: 80%;
    position: fixed;
    top: 50rpx;
    left: 50%;
    margin-left: -40%;
    z-index: 10000;
}
.baocun {
    display: block;
    width: 80%;
    height: 80rpx;
    padding: 0;
    line-height: 80rpx;
    text-align: center;
    position: fixed;
    bottom: 50rpx;
    left: 10%;
    background: #ffe200;
    color: #333;
    font-size: 32rpx;
    border-radius: 44rpx;
}
button[class='baocun']::after {
    border: 0;
}
</style>
